Add java really this time!
[f432xdd.git] / java / Graph Editor / src / interfaces / GraphPanel.java
blobfc5148dc1c8d08c84b3743428791a15365c7de55
1 package interfaces;
2 import java.awt.Color;
3 import java.awt.Graphics;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Observable;
7 import java.util.Observer;
9 import javax.swing.*;
11 import controllers.SelectionController;
12 import models.GraphEdge;
13 import models.GraphModel;
14 import models.GraphVertex;
16 public class GraphPanel extends JPanel implements Observer{
17 private static final long serialVersionUID = 1388592199136335514L;
18 private GraphModel model;
19 private SelectionController selectionController;
21 public GraphPanel(GraphModel model){
22 this.setSize(500, 400);
23 this.setBackground(Color.BLUE);
24 this.model = model;
26 GraphVertex a = new GraphVertex();
27 GraphVertex b = new GraphVertex();
28 GraphVertex c = new GraphVertex();
30 GraphEdge edge1 = new GraphEdge(new GraphVertex[] {a,b});
31 GraphEdge edge2 = new GraphEdge(new GraphVertex[] {b,c});
32 GraphEdge edge3 = new GraphEdge(new GraphVertex[] {c,a});
34 model.addGraph(a,b,c);
35 model.addEdge(edge1,edge2,edge3);
36 model.addObserver(this);
39 public void setModel(GraphModel model){
40 if(this.model != null){
41 this.model.deleteObserver(this);
43 this.model = model;
44 model.addObserver(this);
45 selectionController.setModel(model);
48 int[] calculateOrigenPointOfLine(GraphVertex a, GraphVertex b)
50 int[] returnvalue = new int[2];
51 if(a.getX() > (b.getX() + a.getWidth())){
52 // Rechterzijkant gebruiken
54 returnvalue[0] = a.getX();
55 returnvalue[1] = a.getY()+ (a.getHeight() / 2);
56 }else{
57 if(b.getX() > (a.getX() + a.getWidth())){
58 // Linkerzijkant gebruiken
59 returnvalue[0] = a.getX()+a.getWidth();
60 returnvalue[1] = a.getY()+ (a.getHeight() / 2);
61 }else{
62 if(b.getY() > a.getY()){
63 // bovenkant gebruiken
64 returnvalue[0] = a.getX()+ (a.getWidth()/2);
65 returnvalue[1] = a.getY()+ a.getHeight();
66 }else{
67 // onderkant gebruiken
68 returnvalue[0] = a.getX()+ (a.getWidth()/2);
69 returnvalue[1] = a.getY();
73 return returnvalue;
76 public void addNode()
78 model.addGraph(new GraphVertex());
81 public void deleteSelectedNode()
83 model.removeGraph(selectionController.getSelectedVertex());
84 selectionController.removeSelected();
87 public void paintComponent(Graphics g)
89 g.setColor(Color.white);
90 g.fillRect(0, 0, getWidth(), getHeight());
92 GraphVertex[] objects = model.getVertexes();
93 for(int i=0; i < objects.length; i++){
94 GraphVertex o = objects[i];
95 if(o.isSelected()){
96 g.setColor(Color.red);
97 }else{
98 g.setColor(Color.black);
100 g.drawRect(o.getX(), o.getY(),o.getWidth(), o.getHeight());
101 g.setColor(Color.black);
102 g.drawString(o.getName(), o.getX()+10, o.getY()+25);
104 GraphEdge[] edges = model.getEdges();
105 g.setColor(Color.cyan);
106 for(int i=0; i < edges.length; i++){
107 GraphEdge edge = edges[i];
108 GraphVertex[] vertexes = edge.getNodes();
109 int[] origen_1 = calculateOrigenPointOfLine(vertexes[0], vertexes[1]);
110 int[] origen_2 = calculateOrigenPointOfLine(vertexes[1], vertexes[0]);
111 g.drawLine(origen_1[0], origen_1[1], origen_2[0], origen_2[1]);
115 @Override
116 public void update(Observable arg0, Object arg1) {
117 if(arg0 == model){
118 System.out.println("Refresh @ graphpanel" + arg0.toString());
119 repaint();
120 }else{
121 System.out.println("FOUT: onbekend object riep GraphPanel.update aan: " + arg0.toString());